options validvarname=upcase;
%let domain=adex;
%include "&studyroot\Work\Programs\macros\template\TDD_formats.sas";

%get_data(lst=ex,supp=Y,lib=sdtm,pre=sdtm);
%get_data(pre=adam,lib=adam,lst=adsl);
%get_sdtm_se;

proc sql;
create table code1 as select distinct excat from sdtmex where excat ne " "  order by excat;
create table code2 as select distinct exscat from sdtmex where exscat ne " " order by exscat;
quit;

data code10;
set code1;
by excat ;
if first.excat then do; 
parcat1n+1; end;
retain parcat1n;
run;

data code20;
set code2;
by exscat ;
if first.exscat then do; 
parcat2n+10; end;
retain parcat2n;
run;

proc sort data=sdtmex; by excat; run;
data sdtmex; merge sdtmex code10; by excat; run;

proc sort data=sdtmex; by exscat; run;
data sdtmex; merge sdtmex code20; by exscat; run;

proc sort data=sdtmex; by usubjid exseq; run;


data adex;
   label
      parcat1="Parameter Category 1" 
      parcat1n="Parameter Category 1 (N)"
      parcat2="Parameter Category 2" 
      parcat2n="Parameter Category 2 (N)"
      astdt="Analysis Start Date"
      asttm="Analysis Start Time"
      ASTDTM="Analysis Start Date/Time"
      astdy="Analysis Start Relative Day"
	      aendt="Analysis End Date"
	      aentm="Analysis End Time"
	      AENDTM="Analysis End Date/Time"
	      aendy="Analysis End Relative Day"
      avisit="Analysis Visit"
      avisitn="Analysis Visit (N)"
/*      aval="Analysis Value"*/
/*      avalc="Analysis Value (C)"*/
      anl01fl="Analysis Record Flag 01"
      SRCDOM	="Source Data"
/*      SRCVAR	="Source Variable"*/
      SRCSEQ	="Source Sequence Number"
      DOSEA	="Actual Product Dose"
	  DOSEU	="Actual Product Dose Units"
	  ADUR="Duration Product Use"
	  	  ADURU="Duration Product Use Units"

	  ADURM="Duration Product Use (min)"
	  ;
   format astdt aendt date9. asttm aentm time8. astdtm aendtm DATETIME18.;
   length avisit $50. parcat1 parcat2 $200. anl01fl $1. SRCDOM $2. aduru $10. adur adurm 8.;
   merge sdtmex(in=a) adamadsl(keep=usubjid subjid trt0: tr0: trt: armcd actarmcd enrlfl saffl randafl randbfl complfl pkfl pdfl);
   by usubjid;
   if a;
 
   srcdom=domain;
   srcseq=exseq;
   if exstat ne "NOT DONE" then anl01fl="Y";
   		else anl01fl=" ";
parcat1=strip(excat);
parcat2=strip(exscat);

   avisitn=int(visitnum);
   avisit=strip(visit);

   doseu=strip(exdosu);
   dosea=exdose;
/*   if exdostxt ne " " then dosea=input(exdostxt,best.);*/
/*   else dosea=exdose; */

/*   avalc=strip(put(exdose,??best.));*/

/*   if length(exstdtc)=16 then do; exstdtc=strip(exstdtc)||":00"; end;*/
/*   if length(exsentc)=16 then do; exendtc=strip(exendtc)||":00"; end;*/

   if length(exstdtc)>=10 then do;
         astdy=EXSTDY;
         astdt=input(substr(exstdtc,1,10),yymmdd10.);
/*         if 10<length(exstdtc)<=16 then do;*/
/*               asttm=input(substr(exstdtc,12,5),time8.);*/
/*               AstDTM=input(exstdtc, E8601DT.);         */
/*         end;*/
		 if length(exstdtc)=>16 then do;
               asttm=input(substr(exstdtc,12),time8.);
               AstDTM=input(exstdtc, E8601DT.);         
         end;
      end;

   if length(exendtc)>=10 then do;
         aendy=EXENDY;
         aendt=input(substr(exendtc,1,10),yymmdd10.);
/*         if 10<length(exendtc)<=16 then do;*/
/*               aentm=input(substr(exendtc,12,5),time8.);*/
/*               AENDTM=input(exendtc, E8601DT.);         */
/*         end;*/
		 if length(exendtc)=>16 then do;
               aentm=input(substr(exendtc,12),time8.);
               AENDTM=input(exendtc, E8601DT.);         
         end;
      end;
if ASTDTM ne . and AENDTM ne . then do;
adur=AENDTM-ASTDTM;
aduru="sec";
adurm=adur/60;
end;
format adur best.; format  adurm 6.3;
run;

proc format;
invalue trtf
"Study Product A in Part A"=1
"Study Product B in Part A"=2
"Study Product C in Part A"=3
"Study Product A in Part B"=4
"Study Product B in Part B"=5
"Study Product C in Part B"=6

"Study Product A"=10
"Study Product B"=20
"Study Product C"=30

;
run;


data adex1;
label 
      APERIOD	="Period"
      APERIODC	="Period (C)"
	  trtp="Planned Product"
      trtpn="Planned Product (N)"
      TRTA="Actual Product"
      TRTAN="Actual Product (N)"
	  aform="Product Dose Form"
/*	  exprd="Actual Product Type";*/
;
	  /*(keep=subjid  adtm avisitn atpt TR01SDTM TR01EDTM TR02SDTM TR02EDTM TR03SDTM TR03EDTM TR04SDTM TR04EDTM TR05SDTM TR05EDTM TR06SDTM TR06EDTM d j);*/
length d aperiod trtpn trtan 8. aform aperiodc $40. trtp trta $100.; 
set adex;
array start {6} TR01SDTM  TR02SDTM  TR03SDTM  TR04SDTM  TR05SDTM  TR06SDTM   ;
array end {6} TR01EDTM  TR02EDTM  TR03EDTM  TR04EDTM  TR05EDTM  TR06EDTM  ;
do j=1 to 6;
if  astdtm>= start{j} and aendtm <= end{j} then do; d=j; leave;  end;
end;
aperiod=d;
if d ne . then aperiodc="Period 0"||strip(put(d,best.));
else aperiodc=" ";
if extrt="Very Low Nicotine Cigarettes" then _extrt="A";
else if extrt="Usual Brand Cigarettes" then _extrt="B";
else if extrt="Nicorette Gum" then _extrt="C";

/*_trtp="Study Product "||strip(_extrt)||" in "||strip(propcase(exgrpid));*/
_trtp="Study Product "||strip(_extrt);

if exgrpid="PART A" then do;
/*trtp=strip(extrt)||" in "||strip(propcase(exgrpid));*/
trtp=strip(extrt);
end;
if exgrpid="PART B" then do;
/*trtp=strip(extrt)||" in "||strip(propcase(exgrpid));*/
trtp=strip(extrt);
end;

trta=strip(trtp);
trtpn=input(_trtp,trtf.);
/*trtan=input(trta,trtf.);*/
/*trtpn=d;*/
trtan=trtpn;
aform=strip(exdosfrm);
/*if exprdt ne " " then exprd=strip(exprdt);*/
/*else if exprdb ne " " then exprd=strip(exprdb);*/
/*else exprd=" ";*/
run;

%ADSL_varS;

Proc SQL;
   create table adamw.adex(label = "Analysis Exposure Dataset" sortedby =STUDYID USUBJID srcseq) as
   SELECT studyid, usubjid, subjid, armcd, actarmcd, 
/*saffl, complfl, randfl, pkfl, */
/*enrlfl,enrlfn,saffl,saffn,randfl,randfn,complfl,complfn,pkfl,pkfn,pdfl,pdfn,*/
		enrlfl,saffl,randafl,randbfl,complfl,pkfl,pdfl,
TRTSDT,TRTSDTM,TRTEDT,TRTEDTM,
TRTPN, TRTP, TRTAN, TRTA, AVISITN,AVISIT,
/*APERIOD,APERIODC,*/
parcat1, parcat1n, parcat2, parcat2n, 
ASTDT, ASTTM, ASTDTM, ASTDY, AENDT, AENTM, AENDTM, AENDY, 
dosea, doseu, aform, adur, aduru, ANL01FL, srcdom, srcseq, exspid, exgrpid, 
/*exroute, exloc, */
exbev
/*, exstat*/
   FROM adex1 
   order by STUDYID, USUBJID, srcseq
   ;
QUIT;

%clrw;